package service

import (
	"dsc-server/domain/req"
	"dsc-server/domain/resp"
	model2 "dsc-server/pkg/model"
	"errors"
	"github.com/liangboceo/yuanboot/pkg/datasources/mysql"
	"gorm.io/gorm"
	"time"
)

type DbService struct {
	db *gorm.DB
}

func NewDbService(source *mysql.MySqlDataSource) *DbService {
	db := mysql.NewGormDb(source)
	_ = db.Set("gorm:table_options", "COMMENT='缓存设置表'").AutoMigrate(&model2.CacheSetting{})
	_ = db.Set("gorm:table_options", "COMMENT='缓存访问历史表'").AutoMigrate(&model2.CacheViewHistory{})
	_ = db.Set("gorm:table_options", "COMMENT='API配置表'").AutoMigrate(&model2.ApiConfig{})
	_ = db.Set("gorm:table_options", "COMMENT='数据源配置表'").AutoMigrate(&model2.Datasource{})
	_ = db.Set("gorm:table_options", "COMMENT='API权限关联表'").AutoMigrate(&model2.ApiAuth{})
	_ = db.Set("gorm:table_options", "COMMENT='API分组表'").AutoMigrate(&model2.ApiGroup{})
	_ = db.Set("gorm:table_options", "COMMENT='访问令牌表'").AutoMigrate(&model2.Token{})
	_ = db.Set("gorm:table_options", "COMMENT='系统用户表'").AutoMigrate(&model2.User{})
	return &DbService{db: db}
}

func (dbService *DbService) SaveCacheSetting(setting *model2.CacheSetting) error {
	setting.CreateTime = time.Now()
	setting.UpdateTime = time.Now()
	var existModel = &model2.CacheSetting{}
	result := dbService.db.Model(&setting).Where(&model2.CacheSetting{
		ServerName: setting.ServerName,
		KeyName:    setting.KeyName,
	}).Limit(1).Find(&existModel)
	if result.Error != nil || existModel.ID <= 0 {
		if errors.Is(result.Error, gorm.ErrRecordNotFound) {
			result := dbService.db.Create(&setting)
			if result.Error != nil {
				return result.Error
			}
		} else if existModel.ID <= 0 {
			result := dbService.db.Create(&setting)
			if result.Error != nil {
				return result.Error
			}
		}
	} else {
		setting.ID = existModel.ID
		result := dbService.db.Save(&setting)
		if result.Error != nil {
			return result.Error
		}
	}
	return nil
}

func (dbService *DbService) GetCacheSetting(req *req.CacheGetRuleReq) (*resp.CacheGetRuleResp, error) {
	var result = &resp.CacheGetRuleResp{BasePageResp: resp.BasePageResp{Page: req.PageNum}}
	table := &model2.CacheSetting{
		ServerName: req.ServerName,
		KeyName:    req.KeyName,
		CommonModel: model2.CommonModel{
			IsDeleted:  0,
			UpdateTime: req.UpdateTime,
			CreateTime: req.CreateTime,
		},
	}
	tx := dbService.db.Model(table).Where(table)
	tx.Count(&result.Total)
	err := tx.Offset((req.PageNum - 1) * req.PageSize).Limit(req.PageSize).Find(&result.Data).Error
	return result, err
}

func (dbService *DbService) GetCacheView(req *req.CacheGetViewReq) (*resp.CacheGetViewResp, error) {
	var result = &resp.CacheGetViewResp{BasePageResp: resp.BasePageResp{Page: req.PageNum}}
	table := &model2.CacheViewHistory{
		ServerName: req.ServerName,
		KeyName:    req.KeyName,
		Action:     req.Action,
		CommonModel: model2.CommonModel{
			IsDeleted:  0,
			UpdateTime: req.UpdateTime,
			CreateTime: req.CreateTime,
		},
	}
	tx := dbService.db.Model(table).Where(table)
	tx.Count(&result.Total)
	err := tx.Offset((req.PageNum - 1) * req.PageSize).Order("id desc").Limit(req.PageSize).Find(&result.Data).Error
	return result, err
}
